package cn.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sxt.sys.constast.SYS_Constast;
import cn.sxt.sys.domain.Permission;
import cn.sxt.sys.service.PermissionService;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.utils.TreeNode;
import cn.sxt.sys.vo.PermissionVo;

@Controller
@RequestMapping("permission")
public class PermisssonController {

	@Autowired
	private PermissionService permissionService;

	// 跳到permissionManager.jsp
	@RequestMapping("toPermissionManager")
	public String toPermissionManager() {
		return "system/permission/permissionManager";
	}

	// permissionManager.jsp加载 左边
	@RequestMapping("toPermissionLeft")
	public String toPermissionLeft() {
		return "system/permission/permissionLeft";
	}

	// permissionManager.jsp加载 右边
	@RequestMapping("toPermissionRight")
	public String toPermissionRight() {
		return "system/permission/permissionRight";
	}

	// 菜单数据，分页，模糊
	@RequestMapping("queryAllPermission")
	@ResponseBody
	public DataGridView queryAllPermission(PermissionVo permissionVo) {
		permissionVo.setType(SYS_Constast.PERMISSION_TYPE_PERMISSION);
		return permissionService.queryAllPermission(permissionVo);
	}

	// 跳添加页面
	@RequestMapping("toAddPermission")
	public String toAddPermission() {
		return "system/permission/permissionAdd";
	}

	// 添加
	@RequestMapping("addPermission")
	@ResponseBody
	public Map<String, Object> addPermission(PermissionVo permissionVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			permissionVo.setType(SYS_Constast.PERMISSION_TYPE_PERMISSION);
			int i = permissionService.addPermission(permissionVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdatePermission")
	public String toUpdatePermission(PermissionVo permissionVo, Model model) {
		Permission permission = permissionService.queryPermissionById(permissionVo.getId());
		model.addAttribute("permission", permission);
		return "system/permission/permissionUpdate";
	}

	// 修改
	@RequestMapping("updatePermission")
	@ResponseBody
	public Map<String, Object> updatePermission(PermissionVo permissionVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = permissionService.updatePermission(permissionVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除
	@RequestMapping("deletePermission")
	@ResponseBody
	public Map<String, Object> deletePermission(PermissionVo permissionVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = permissionService.deletePermissionById(permissionVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除日志
	@RequestMapping("batchDeletePermission")
	@ResponseBody
	public Map<String, Object> batchDeletePermission(PermissionVo permissionVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = permissionVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					permissionVo.setId(ids[i]);
					int j = permissionService.deletePermissionById(permissionVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

}
